package com.stenio.fancat.service;

import java.text.ParseException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stenio.fancat.domain.ActOperLog;
import com.stenio.fancat.domain.bo.ActOperLogBO;
import com.stenio.fancat.form.ActOperLogForm;
import com.stenio.fancat.mapper.ActOperLogMapper;

/**
 * 消息模块服务实现类
 */
@Service
public class MessageService {

	@Autowired
	private ActOperLogMapper actOperLogMapper;

	/**
	 * 查询用户的新消息，目前仅有账号变动消息
	 * 
	 * @param act
	 *            活动实体
	 * @return
	 * @throws ParseException
	 */
	public List<ActOperLogBO> queryUserMsg(ActOperLogForm actOperLogForm) throws ParseException {
		if (null == actOperLogForm || null == actOperLogForm.getUserId()) {
			throw new RuntimeException("参数不能为空");
		}
		// 获取用户未读收入消息
		ActOperLog actOperLog = new ActOperLog();
		actOperLog.setUserId(actOperLogForm.getUserId());
		// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// Date date = sdf.parse(actOperLogForm.getCreateTime());
		// actOperLog.setCreateTime(date);
		List<ActOperLogBO> list = actOperLogMapper.selectByUserId(actOperLog);
		if (null != list && list.size() > 0) {
			// 将未读的记录更新为已读
			ActOperLog entity = new ActOperLog();
			entity.setRead(1);
			StringBuffer buffer = new StringBuffer();
			for (ActOperLogBO bo : list) {
				buffer.append(bo.getId()).append(",");
			}
			buffer.deleteCharAt(buffer.length() - 1);
			actOperLogMapper.update(entity, new EntityWrapper<ActOperLog>().in(ActOperLog.ID, buffer.toString()));
		}
		return list;
	}

}
